﻿Imports System.IO

Public Class frmReminder

    Private Sub frmReminder_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click
        Me.Cursor = Cursors.WaitCursor
        SaveFileDialog1.Filter = "Excel files|*.xls;*.xlsx"
        SaveFileDialog1.InitialDirectory = "C:\"
        SaveFileDialog1.FileName = "Reminder Listing.xls"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            GridEXExporter1.ExportMode = Janus.Windows.GridEX.ExportMode.CheckedRows
            Dim ReportFile As String = SaveFileDialog1.FileName
            Dim stream As New FileStream(ReportFile, FileMode.Create)
            Try
                GridEXExporter1.Export(stream)
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            Finally
                stream.Flush()
                stream.Dispose()
                MessageBox.Show("Export Completed!")
            End Try
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnRun_Click(sender As System.Object, e As System.EventArgs) Handles btnRun.Click
        Me.Cursor = Cursors.WaitCursor
        Dim fn As New Functions
        Dim dt As DataTable = fn.DoQueryReturnDT("sp_AI_BPBalance '" + DateTimePicker1.Value.Date.ToString("MM/dd/yyyy") + "'")
        GridEX1.DataSource = dt
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

            Me.Cursor = Cursors.WaitCursor
            Dim opr As New oPrint
            Dim PrinterName As String = ""
            Dim PDFFile As String = ""
            Dim ParaValue As String = ""
            Dim ParaName As String = "@Date;@CardCode;@Layout;@PaymentDate"


            Dim SQLwd As String = ""
            Dim ors As SAPbobsCOM.Recordset
            ors = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            ors.DoQuery("Select * from [@SAPWD]")
            If ors.RecordCount > 0 Then
                SQLwd = ors.Fields.Item("Name").Value.ToString()
            Else
                MessageBox.Show("Please setup SQL password!")
                Return
            End If
            Dim Connection As String = PublicVariable.oCompany.Server + ";" + PublicVariable.oCompany.CompanyDB + ";" + PublicVariable.oCompany.DbUserName + ";" + SQLwd
            Dim PDF As Boolean = False
            Dim PDFolder As String = ""

            If ComboBox1.Text = "PDF" Then
                PDF = True
                If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                    PDFolder = FolderBrowserDialog1.SelectedPath
                Else
                    Return
                End If
            Else
                If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                    PrinterName = PrintDialog1.PrinterSettings.PrinterName
                Else
                    Return
                End If
            End If


            For Each row As Janus.Windows.GridEX.GridEXRow In GridEX1.GetCheckedRows
                ParaValue = DateTimePicker1.Value.Date.ToString("MM/dd/yyyy") + ";" _
                            + row.Cells("CardCode").Value.ToString + ";" _
                            + row.Cells("LayOut").Value.ToString + ";" _
                            + DateTimePicker2.Value.Date.ToString("MM/dd/yyyy")
                PDFFile = PDFolder + "\" + row.Cells("CardCode").Value.ToString + ".pdf"
                opr.PrintCrystalReport(Application.StartupPath + "\ReminderLetter.rpt", PrinterName, Connection, ParaName, ParaValue, PDF, PDFFile)
            Next

            MessageBox.Show("Generate Completed!")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Me.Cursor = Cursors.Default
    End Sub
End Class
